@gradingform @gradingform_metaagm
Feature: A metacriteria forms as well as whole Meta form can be published as a templates.
  In order to publish the metacriterion form
  As a teacher
  I need to press "Publish as template" when metacriteria form is defined.

  In order to publish the whole Meta form
  As a teacher
  I need to press "Publish the form as a new template" when the Meta form is active.

  Background:
    Given the following "users" exist:
      | username | firstname | lastname | email            |
      | teacher1 | Teacher   | 1        | teacher1@asd.com |
    And the following "system role assigns" exist:
      | user     | role    | contextlevel | reference |
      | teacher1 | manager | System       |           |
    And the following "courses" exist:
      | fullname | shortname | format |
      | Course 1 | C1        | topics |
    And the following "course enrolments" exist:
      | user     | course | role           |
      | teacher1 | C1     | editingteacher |
    #And I log in as "admin"
    #And I set the following administration settings values:
    #  | Debug messages | 0 |
    #  | debugdisplay   | 0 |
    #And I log out

  Scenario: Publish metacriterion grading form
    Given the following "activities" exist:
      | activity | course | idnumber | name                   | intro | advancedgradingmethod_submissions |
      | assign   | C1     | A1       | Test assignment 1 name | TA1   | metaagm                           |
      | assign   | C1     | A2       | Test assignment 2 name | TA2   | rubric                            |
    And I log in as "teacher1"
    And I follow "Course 1"
    And I go to "Test assignment 1 name" advanced grading definition page
    And I create the metacriterion "Test metacriterion 1" with grade weight "1.0" and grading method "Rubric"
    And I create a definition for metacriterion "Test metacriterion 1"
    And I set the following fields to these values:
      | Name        | Metacriterion 1 rubric  |
      | Description | Rubric test description |
    And I define the following rubric:
      | Rubric Criterion 1 | Level 11 |  1 | Level 12 | 20 | Level 13 | 40 | Level 14 | 50 |
      | Rubric Criterion 2 | Level 21 | 10 | Level 22 | 20 | Level 23 | 30 |
      | Rubric Criterion 3 | Level 31 |  5 | Level 32 | 20 |
    And I press "Save rubric and make it ready"
    When I share the grading form of metacriterion "Test metacriterion 1"
    Then I go to "Test assignment 2 name" advanced grading page
    And I follow "Create new grading form from a template"
    And I should see "Metacriterion 1 rubric"

  Scenario: Use template for metacriterion grading form
    Given the following "activities" exist:
      | activity | course | idnumber | name                   | intro | advancedgradingmethod_submissions |
      | assign   | C1     | A1       | Test assignment 1 name | TA1   | metaagm                           |
      | assign   | C1     | A2       | Test assignment 2 name | TA2   | rubric                            |
    And I log in as "teacher1"
    And I follow "Course 1"
    And I go to "Test assignment 2 name" advanced grading definition page
    And I set the following fields to these values:
      | Name        | Assignment 2 rubric     |
      | Description | Rubric test description |
    And I define the following rubric:
      | Rubric Criterion 1 | Level 11 |  1 | Level 12 | 20 | Level 13 | 40 | Level 14 | 50 |
      | Rubric Criterion 2 | Level 21 | 10 | Level 22 | 20 | Level 23 | 30 |
      | Rubric Criterion 3 | Level 31 |  5 | Level 32 | 20 |
    And I press "Save rubric and make it ready"
    And I publish "Test assignment 2 name" grading form definition as a public template
    When I go to "Test assignment 1 name" advanced grading definition page
    And I create the metacriterion "Test metacriterion 1" with grade weight "1.0" and grading method "Rubric"
    And I set metacriterion "Test metacriterion 1" to use "Assignment 2 rubric" grading form
    Then I should see "Assignment 2 rubric"

  Scenario: Publish Meta form
    Given the following "activities" exist:
      | activity | course | idnumber | name                   | intro | advancedgradingmethod_submissions |
      | assign   | C1     | A1       | Test assignment 1 name | TA1   | metaagm                           |
      | assign   | C1     | A2       | Test assignment 2 name | TA2   | metaagm                           |
    And I log in as "teacher1"
    And I follow "Course 1"
    And I go to "Test assignment 1 name" advanced grading definition page
    And I create the metacriterion "Test metacriterion 1" with grade weight "1.0" and grading method "Rubric"
    And I create the metacriterion "Test metacriterion 2" with grade weight "2.0" and grading method "Marking guide"
    And I create a definition for metacriterion "Test metacriterion 1"
    And I set the following fields to these values:
      | Name        | Metacriterion 1 rubric  |
      | Description | Rubric test description |
    And I define the following rubric:
      | Rubric Criterion 1 | Level 11 |  1 | Level 12 | 20 | Level 13 | 40 | Level 14 | 50 |
      | Rubric Criterion 2 | Level 21 | 10 | Level 22 | 20 | Level 23 | 30 |
      | Rubric Criterion 3 | Level 31 |  5 | Level 32 | 20 |
    And I press "Save rubric and make it ready"
    And I create a definition for metacriterion "Test metacriterion 2"
    And I set the following fields to these values:
      | Name        | Metacriterion 1 guide  |
      | Description | Guide test description |
    And I define the following marking guide:
      | Guide Criterion 1 | For Students 1... | For Markers 1... | 30 |
      | Guide Criterion 2 | For Students 2... | For Markers 2... | 70 |
    And I press "Save marking guide and make it ready"
    And I publish "Test assignment 1 name" grading form definition as a public template
    And I go to "Test assignment 1 name" advanced grading definition page
    And I delete metacriterion "Test metacriterion 1"
    And I delete metacriterion "Test metacriterion 2"
    When I go to "Test assignment 2 name" advanced grading page
    And I follow "Create new grading form from a template"
    Then I should see "Rubric Criterion 1"
    And I should see "Rubric Criterion 2"
    And I should see "Rubric Criterion 3"
    And I should see "Guide Criterion 1"
    And I should see "Guide Criterion 2"
    When I follow "Edit settings"
    And I set the following fields to these values:
      | Grading method  | Rubric |
    And I press "Save and display"
    And I go to "Test assignment 2 name" advanced grading page
    And I follow "Create new grading form from a template"
    Then I should see "Rubric Criterion 1"
    And I should see "Rubric Criterion 2"
    And I should see "Rubric Criterion 3"
    When I follow "Edit settings"
    And I set the following fields to these values:
      | Grading method  | Marking guide |
    And I press "Save and display"
    And I go to "Test assignment 2 name" advanced grading page
    And I follow "Create new grading form from a template"
    Then I should see "Guide Criterion 1"
    And I should see "Guide Criterion 2"

  Scenario: Use Meta template
    Given the following "activities" exist:
      | activity | course | idnumber | name                   | intro | advancedgradingmethod_submissions |
      | assign   | C1     | A1       | Test assignment 1 name | TA1   | metaagm                           |
      | assign   | C1     | A2       | Test assignment 2 name | TA2   | metaagm                           |
    And I log in as "teacher1"
    And I follow "Course 1"
    And I go to "Test assignment 1 name" advanced grading definition page
    And I create the metacriterion "Test metacriterion 1" with grade weight "1.0" and grading method "Rubric"
    And I create the metacriterion "Test metacriterion 2" with grade weight "2.0" and grading method "Marking guide"
    And I create a definition for metacriterion "Test metacriterion 1"
    And I set the following fields to these values:
      | Name        | Metacriterion 1 rubric  |
      | Description | Rubric test description |
    And I define the following rubric:
      | Rubric Criterion 1 | Level 11 |  1 | Level 12 | 20 | Level 13 | 40 | Level 14 | 50 |
      | Rubric Criterion 2 | Level 21 | 10 | Level 22 | 20 | Level 23 | 30 |
      | Rubric Criterion 3 | Level 31 |  5 | Level 32 | 20 |
    And I press "Save rubric and make it ready"
    And I create a definition for metacriterion "Test metacriterion 2"
    And I set the following fields to these values:
      | Name        | Metacriterion 1 guide  |
      | Description | Guide test description |
    And I define the following marking guide:
      | Guide Criterion 1 | For Students 1... | For Markers 1... | 30 |
      | Guide Criterion 2 | For Students 2... | For Markers 2... | 70 |
    And I press "Save marking guide and make it ready"
    And I publish "Test assignment 1 name" grading form definition as a public template
    # Delete current Meta form.
    And I go to "Test assignment 1 name" advanced grading definition page
    And I delete metacriterion "Test metacriterion 1"
    And I delete metacriterion "Test metacriterion 2"
    And I go to "Test assignment 1 name" advanced grading page
    And I follow "Delete the currently defined form"
    And I press "Continue"
    When I set "Test assignment 2 name" activity to use "Shared template" grading form
    # Check the template was copied.
    Then I go to "Test assignment 2 name" advanced grading page
    And I should not see "Publish the form as a new template"
    And I should see "Rubric Criterion 1"
    And I should see "Rubric Criterion 2"
    And I should see "Rubric Criterion 3"
    And I should see "Guide Criterion 1"
    And I should see "Guide Criterion 2"
    # When I delete all templates.
    When I go to "Test assignment 1 name" advanced grading page
    And I follow "Create new grading form from a template"
    And I follow "Delete"
    And I press "Continue"
    And I follow "Edit settings"
    And I set the following fields to these values:
      | Grading method  | Marking guide |
    And I press "Save and display"
    And I go to "Test assignment 1 name" advanced grading page
    And I follow "Create new grading form from a template"
    And I follow "Delete"
    And I press "Continue"
    And I follow "Edit settings"
    And I set the following fields to these values:
      | Grading method  | Rubric |
    And I press "Save and display"
    And I go to "Test assignment 1 name" advanced grading page
    And I follow "Create new grading form from a template"
    And I follow "Delete"
    And I press "Continue"
    # Check Assignment 2 form still work correctly.
    Then I go to "Test assignment 2 name" advanced grading page
    And I should see "Publish the form as a new template"
    And I should see "Rubric Criterion 1"
    And I should see "Rubric Criterion 2"
    And I should see "Rubric Criterion 3"
    And I should see "Guide Criterion 1"
    And I should see "Guide Criterion 2"
  #@javascript
  Scenario: Delete metacriterion grading form template (child of Meta template)
    Given the following "activities" exist:
      | activity | course | idnumber | name                   | intro | advancedgradingmethod_submissions |
      | assign   | C1     | A1       | Test assignment 1 name | TA1   | metaagm                           |
      | assign   | C1     | A2       | Test assignment 2 name | TA2   | rubric                            |
    And I log in as "teacher1"
    And I follow "Course 1"
    And I go to "Test assignment 1 name" advanced grading definition page
    And I create the metacriterion "Test metacriterion 1" with grade weight "1.0" and grading method "Rubric"
    And I create the metacriterion "Test metacriterion 2" with grade weight "2.0" and grading method "Marking guide"
    And I create a definition for metacriterion "Test metacriterion 1"
    And I set the following fields to these values:
      | Name        | Metacriterion 1 rubric  |
      | Description | Rubric test description |
    And I define the following rubric:
      | Rubric Criterion 1 | Level 11 |  1 | Level 12 | 20 | Level 13 | 40 | Level 14 | 50 |
      | Rubric Criterion 2 | Level 21 | 10 | Level 22 | 20 | Level 23 | 30 |
      | Rubric Criterion 3 | Level 31 |  5 | Level 32 | 20 |
    And I press "Save rubric and make it ready"
    And I create a definition for metacriterion "Test metacriterion 2"
    And I set the following fields to these values:
      | Name        | Metacriterion 1 guide  |
      | Description | Guide test description |
    And I define the following marking guide:
      | Guide Criterion 1 | For Students 1... | For Markers 1... | 30 |
      | Guide Criterion 2 | For Students 2... | For Markers 2... | 70 |
    And I press "Save marking guide and make it ready"
    And I publish "Test assignment 1 name" grading form definition as a public template
    When I go to "Test assignment 2 name" advanced grading page
    And I follow "Create new grading form from a template"
    And I follow "Delete"
    And I press "Continue"
    And I follow "Edit settings"
    And I set the following fields to these values:
      | Grading method  | Meta AGM |
    And I press "Save and display"
    And I set "Test assignment 2 name" activity to use "Shared template" grading form
    And I go to "Test assignment 2 name" advanced grading definition page
    And I should see "Guide Criterion 1"
    And I should see "Guide Criterion 2"
    And I should see "The advanced grading form is not ready at the moment"